# !/usr/bin/env python
# -*- coding:utf-8 -*-

import logging
from time import time
def timeshow(func):
    """计时函数"""
    def newfunc(*arg, **kw):
        t1 = time()
        res = func(*arg, **kw)
        t2 = time()
        logging.info(f"{func.__name__: >10} : {t2 - t1:.6f} sec")
        return res

    return newfunc

# 查询用例、模块数
#sql1='select count(*) from testcase where status = 10'
#sql2='select count(*) from module where status = 10'

# 查询子模块id，传入：moduleid/%
#sql3="select moduleid from module where parent_path like ? "

#查询用例，无模块ID时，传入：状态码值
sql4='''SELECT
        testcase.caseid,
        testcase.title,
        testcase.preconditions,
        testcase.step,
        testcase.expect,
        testcase.keyword,
        module.modulename AS modulename,
        codes1.value AS priority,
        codes2.value AS types,
        testcase.remarks,
        testcase.modificationdate
    FROM
        testcase
    LEFT JOIN
        codes AS codes1 ON testcase.priority = codes1.id AND codes1.id BETWEEN 1 AND 4 -- 关联优先级码值表
    LEFT JOIN
        codes AS codes2 ON testcase.types = codes2.id AND codes2.id BETWEEN 20 AND 28 -- 关联类型码值表
    INNER JOIN
        codes AS codes3 ON testcase.status = codes3.id AND codes3.id BETWEEN 10 AND 11
    LEFT JOIN
        module  ON testcase.moduleid = module.moduleid 
        where '''

# 查询用例数量，无模块ID时，传入：状态码值
sql5="select count(*) from testcase join module on testcase.moduleid=module.moduleid where "

# 查询模块、数量，传入：modulename、状态码值
#sql6='select modulename,moduleid,parent_id from module where modulename like ? and status = ?'
#sql7='select count(*) from module where modulename like ? and status = ?'

# 新增用例
#sql8='INSERT INTO testcase VALUES (NULL,?,?,?,?,?,?,?,?,?,?,?,?)'

# 码值转换
codes_dict = {
    'priority': {'冒烟测试':1, '高':2, '中':3, '低':4},
    'status': {'正常':10, '废弃':11},
    'tc_types': {'功能测试':20, '性能测试':21, '配置相关':22, '安装部署':23,'接口测试':24, '安全相关':25, '兼容性测试':26,
                 'UI测试':27, '其它':28},
    'difficulty': {'一眼看出':30, '随便点点':31, '专心设计':32, '反复测试':33, '绞尽脑汁':34, '机缘巧合':35, '无法想象':36},
    'mk_types': {'功能缺陷': 40, '设计缺陷': 41, '业务规则缺陷': 42, '安装部署': 43, '安全相关': 44, '性能问题': 45,
              '接口缺陷': 46, '优化建议': 47, '兼容性缺陷': 48,'其他缺陷': 49},
    'severity': {'轻微':50, '一般':51, '严重':52, '致命':53}
}

# from functools import wraps
# def catch_exception_class_func(func_name=None, return_default=None):
#    '''异常捕获'''
#     def decorate(func):
#         @wraps(func)
#         def wrap(self, *args, **kwargs):
#             if func_name:
#                 name = func_name
#             else:
#                 name = func.__name__
#             try:
#                 if args and kwargs:
#                     return func(self, *args, **kwargs)
#                 elif args:
#                     return func(self, *args)
#                 elif kwargs:
#                     return func(self, **kwargs)
#                 else:
#                     return func(self)
#             except Exception as e:
#                 logging.error(f"执行[{name}]失败, args:{args}, kwargs:{kwargs} 异常:{e}")
#                 return return_default
#         return wrap
#     return decorate


# 根据caseid,查模块id
#sql9="select moduleid from testcase where caseid = ? "

#删除模块、用例
#sql10='delete from module where '
#sql11='delete from testcase where '

# 查模块状态
#sql12="select status from module where moduleid = ?"
#sql15="select status from testcase where caseid= ?"


# 更新模块、用例状态
#sql13="update module set status = ? where moduleid "
#sql14="update testcase set modificationdate= ? ,status = ? where "

# 查询导出用例
# sql16='''SELECT
#         testcase.caseid,
#         testcase.title,
#         testcase.preconditions,
#         testcase.step,
#         testcase.expect,
#         testcase.keyword,
#         module.modulename AS modulename,
#         module.moduleid AS moduleid,
#         codes1.value AS priority,
#         codes2.value AS types,
#         testcase.remarks,
#         testcase.modificationdate
#     FROM
#         testcase
#         INNER JOIN
#         codes AS codes1 ON testcase.priority = codes1.id AND codes1.id BETWEEN 1 AND 4 -- 关联类型码值表
#     INNER JOIN
#         codes AS codes2 ON testcase.types = codes2.id AND codes2.id BETWEEN 20 AND 28 -- 关联难度码值表
#     INNER JOIN
#         codes AS codes3 ON testcase.status = codes3.id AND codes3.id BETWEEN 10 AND 11
#     LEFT JOIN
#         module  ON testcase.moduleid = module.moduleid
#         where testcase.status=10 '''
#
# sql17='select moduleid,parent_id,level,parent_path,modulename from module where status=10 '

# 查询用例类型
#sql18="""select codes.value AS types,count(*) from testcase
        # INNER JOIN
        # codes  ON testcase.types = codes.id AND codes.id BETWEEN 20 AND 28 -- 关联难度码值表
        # where testcase.status = 10
        # group by testcase.types
        # """

# 顶级模块用例数量
# sql21='''SELECT
#     m1.moduleid || ':' || m1.modulename AS modulename_moduleid,
#     COUNT(t.caseid) AS total_cases
# FROM
#     (
#         SELECT
#             moduleid,
#             modulename
#         FROM
#             module
#         WHERE
#             level = 0
#         UNION ALL
#         SELECT
#             SUBSTR(parent_path, 0, INSTR(parent_path, '/') - 1) AS moduleid,
#             m2.modulename
#         FROM
#             module AS m2
#         WHERE
#             m2.parent_path LIKE '%/%' -- 包含至少一个子级的模块路径
#     ) AS m1
# JOIN
#     testcase t ON t.moduleid = m1.moduleid AND t.status = 10
# GROUP BY
#     m1.moduleid;
#     '''
# 顶级模块下子模块用例数
# sql19="""SELECT
#     m1.moduleid || ':' || m1.modulename AS modulename_moduleid,
#     COUNT(t.caseid) AS total_cases
# FROM
#     module AS m1
# LEFT JOIN
#     module AS m2 ON m2.parent_path LIKE m1.moduleid || '/%' -- 使用LIKE操作符匹配子模块路径
# JOIN
#     testcase t ON t.moduleid = m2.moduleid AND t.status = 10 -- 关联测试用例并筛选状态为10的用例
# WHERE
#     m1.level = 0 -- 只选择顶级模块
# GROUP BY
#     m1.moduleid; -- 按顶级模块ID分组计数
#         """

# 查询bug数量
#sql20="select count(*) from bug"

#color=((220, 103, 123),(81, 146, 171),(233, 130, 106),(99, 175, 193),(120, 189, 161),(255, 130, 130),(149, 149, 149))
# color_y=((239, 205, 170),(233, 183, 182),(238, 215, 222),(190, 219, 222),(166, 198, 201),(200, 220, 208),(246, 226, 197),
#         (184, 211, 229),(178, 205, 189))
#color_y2=((177, 217, 224),(149, 200, 217),(153, 207, 203),(118, 198, 200),(99, 175, 193),(120, 189, 161),(154, 205, 169),(160, 221, 216),(183, 225, 215))


#编辑bug信息
#sql22="update bug set title=? ,types=? ,difficulty=?, moduleid=? ,describe=?,think=?,severity=? where bugid=?"

# 新增bug
#sql23="INSERT INTO bug VALUES (NULL,?,?,?,?,?,?,?)"

#bug列表查询
# sql24='''select bugid || ':' || title from bug where'''
# sql25="select count(*) from bug where"

#bug详情查询
# sql26='''select title,codes1.value as types,codes2.value as difficulty,
#         module.modulename || '—ID:' || module.moduleid,
#         describe,think,codes3.value as severity
#         from bug
#     INNER JOIN
#         codes AS codes1 ON bug.types = codes1.id AND codes1.id BETWEEN 40 AND 49 -- 关联类型码值表
#     INNER JOIN
#         codes AS codes2 ON bug.difficulty = codes2.id AND codes2.id BETWEEN 30 AND 36 -- 关联难度码值表
#     INNER JOIN
#         codes AS codes3 ON bug.severity = codes3.id AND codes3.id BETWEEN 50 AND 53
#     LEFT JOIN
#         module  ON bug.moduleid = module.moduleid
#     where bug.bugid = ? '''

# ye_number={0:20,1:30,2:50,3:100,4:1000}
